using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using KY.Mongo;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using KY.Converter;
namespace KY.WebApi.Controllers
{
    
    [ApiController]
    public class AutosetController : ControllerBase
    {
        private IMongoRepository repository;

        public AutosetController(IMongoRepository repository)
        {
            this.repository = repository;
        }
        [Route("dynamicget/{*moduleName}")]
        [HttpGet]
        public object GetSave() {


            Dictionary<string, StringValues> keyValuePairs = Request.Query.ToDictionary(x => x.Key, x => x.Value);
            // [{"Key":"action","Value":"Index"},{"Key":"controller","Value":"Autoset"},{"Key":"moduleName","Value":"vvvv/getdata"}]
            string ModuleAndMethod = Request.RouteValues["moduleName"].ToString();
            var data = new MongoDTO
            {
             
                Value = Guid.NewGuid().ToString(),
               
            };
            data.JsonStr = JsonConvert.SerializeObject(keyValuePairs.DictionaryToObject());
            if (ModuleAndMethod.Split("/").Length == 1)
            {

                data.CollectionName = ModuleAndMethod;
                data.Key = "anonymous";
                repository.InsertAsync(data);
            }
            else if (ModuleAndMethod.Split("/").Length == 2)
            {


                data.CollectionName = ModuleAndMethod.Split("/")[0];
                    data.Key = ModuleAndMethod.Split("/")[1];
                  
                repository.InsertAsync(data);
            }
            else {
                return false;
            }


            return true;
            //if (ModuleAndMethod.Split("/").Length)
            //{

            //}
            //Console.WriteLine(Request.Query.Keys.Count);
            //MongoDTO mongoDTO = new MongoDTO();
            //mongoDTO.CollectionName= Request.RouteValues["moduleName"].ToString();
            //mongoDTO.Key=""
        }


        [Route("dynamicpost/{*moduleName}")]
        [HttpPost]
        public object GetSavepost()
        {


            Dictionary<string, StringValues> getvalues = Request.Query.ToDictionary(x => x.Key, x => x.Value);
            Dictionary<string, StringValues> postvalues = Request.Form.ToDictionary(x => x.Key, x => x.Value);
            var keyValuePairs= getvalues.Concat(postvalues).ToDictionary(x=>x.Key,x=>x.Value);
            // [{"Key":"action","Value":"Index"},{"Key":"controller","Value":"Autoset"},{"Key":"moduleName","Value":"vvvv/getdata"}]
            string ModuleAndMethod = Request.RouteValues["moduleName"].ToString();
            var data = new MongoDTO
            {

                Value = Guid.NewGuid().ToString(),

            };
            data.JsonStr = JsonConvert.SerializeObject(keyValuePairs.DictionaryToObject());
            if (ModuleAndMethod.Split("/").Length == 1)
            {

                data.CollectionName = ModuleAndMethod;
                data.Key = "anonymous";
                repository.InsertAsync(data);
            }
            else if (ModuleAndMethod.Split("/").Length == 2)
            {


                data.CollectionName = ModuleAndMethod.Split("/")[0];
                data.Key = ModuleAndMethod.Split("/")[1];

                repository.InsertAsync(data);
            }
            else
            {
                return false;
            }


            return true;
            //if (ModuleAndMethod.Split("/").Length)
            //{

            //}
            //Console.WriteLine(Request.Query.Keys.Count);
            //MongoDTO mongoDTO = new MongoDTO();
            //mongoDTO.CollectionName= Request.RouteValues["moduleName"].ToString();
            //mongoDTO.Key=""
        }
    }
}